/**
 * helper class to display universe
 */
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

/**
 * @author Master
 * @version $Revision: 1.0 $
 */
public class UniverseScreenPanel extends JPanel{
	
	/**
	 * Field BA.
	 * (value is 8)
	 */
	private static final int BA = 8;

	/**
	 * Field ERSHI.
	 * (value is 20)
	 */
	private static final int ERSHI = 20;

	/**
	 * Field SI.
	 * (value is 4)
	 */
	private static final int SI = 4;
	
	/**
	 * Field SHI.
	 * (value is 10)
	 */
	private static final int SHI = 10;
	
	/**
	 * Field LINGDIANLINGER.
	 * (value is 0.02)
	 */
	private static final float LINGDIANLINGER = 0.02f;
	
	/**
	 * Field panelupdater.
	 */
	public PanelUpdater panelupdater;

	/**
	 * Constructor for UniverseScreenPanel.
	 */
	public UniverseScreenPanel(){
		addMouseListener(new PlanetListener());
		setOpaque(true);
		panelupdater=new PanelUpdater(this, LINGDIANLINGER);
		panelupdater.start();
		repaint();
	}

	/**
	 * Method paintComponent.
	 * @param g Graphics
	 */
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		this.removeAll();
		setBackground(Color.blue);
		
		for (SolarSystem solarsystem: Universe.SolarSystems)
			for (Planet planet: solarsystem.getPlanets()){
				if (planet.getName().equals(PlayerInfo.getCurrentPlanet().getName())) {
					g.setColor(Color.green);
				}
				else {
					g.setColor(Color.red);
					}
				g.fillOval(planet.getX() - SHI, planet.getY() - SHI, ERSHI, ERSHI);
			}
		if (PlayerInfo.getDestination() != null){
			g.setColor(Color.yellow);
			g.fillOval(PlayerInfo.Xx - SI, PlayerInfo.Yy - SI, BA, BA);
		}
		g.setColor(Color.green);
		int val=PlayerInfo.getFuel();
		if (val < SHI) val=SHI;
		g.drawOval(PlayerInfo.CurrentPlanet.getX()
				- val, PlayerInfo.CurrentPlanet.getY() - val, val << 1, val << 1);
	}
	
	/**
	 * Method update.
	 */
	public void update(){

		if (PlayerInfo.getDestination() == null) return;
		final long tot=Math.round(Math.sqrt((PlayerInfo.getDestination().getX() 
				- PlayerInfo.getCurrentPlanet().getX()) *
				(PlayerInfo.getDestination().getX() 
				- PlayerInfo.getCurrentPlanet().getX()) +
				(PlayerInfo.getDestination().getY() - 
				PlayerInfo.getCurrentPlanet().getY()) *
				(PlayerInfo.getDestination().getY() - 
				PlayerInfo.getCurrentPlanet().getY())));
		if (PlayerInfo.Cnt < tot){
			PlayerInfo.Cnt++;
			PlayerInfo.Xx = (int) (PlayerInfo.Cnt *
						   (PlayerInfo.getDestination().getX() -
						   PlayerInfo.getCurrentPlanet().getX()) / tot +
						   PlayerInfo.getCurrentPlanet().getX());
			PlayerInfo.Yy = (int) (PlayerInfo.Cnt *
					   (PlayerInfo.getDestination().getY() -
					   PlayerInfo.getCurrentPlanet().getY()) / tot +
					   PlayerInfo.getCurrentPlanet().getY());
		}else {
			GameInfo.CurrentPlanetListener.go(PlayerInfo.getDestination());
			panelupdater.stop();
		}
		repaint();
		
	}
}
	